package com.ztc.ZtcClassesPriceForSale.web;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.aqgj.User.bean.User;
import com.ztc.ZtcClassesPrice.bean.ZtcClassesPrice;
import com.ztc.ZtcClassesPriceForSale.dao.ZtcClassesPriceForSaleDao;
import com.ztc.ZtcClassesSiteForSale.dao.ZtcClassesSiteForSaleDao;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztc.ZtcClassesForSale.bean.ZtcClassesForSale;
import com.ztc.ZtcClassesForSale.service.ZtcClassesForSaleManager;
import com.ztc.ZtcClassesPriceForSale.bean.ZtcClassesPriceForSale;
import com.ztc.ZtcClassesPriceForSale.service.ZtcClassesPriceForSaleManager;
import com.ztc.ZtcClassesSiteForSale.bean.ZtcClassesSiteForSale;
import com.ztc.ZtcClassesSiteForSale.service.ZtcClassesSiteForSaleManager;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("ztc_classes_price_for_sale_action")
public class ZtcClassesPriceForSaleAction {
    @Autowired
    private ZtcClassesPriceForSaleManager  ztcClassesPriceForSaleManager;
	@Autowired
	private ZtcClassesPriceForSaleDao ztcClassesPriceForSaleDao;
    @Autowired
    private ZtcTransportManager ztcTransportManager;
	@Autowired
	private ZtcClassesForSaleManager  ztcClassesForSaleManager;
    @Autowired
    private ZtcClassesSiteForSaleManager  ztcClassesSiteForSaleManager;
	@Autowired
	private ZtcClassesSiteForSaleDao ztcClassesSiteForSaleDao;
	private static final Logger log = LogManager.getLogger(ZtcClassesPrice.class);
	/**
	 * 根据起始站点名称，结束站点名称，发车日期模糊查找在售班次价格信息
	 * @param startSiteName 起始站点名称
	 * @param endSiteName 结束站点名称
	 * @param departureDate 发车日期
	 * @return 售班次价格信息集合
	 */
    @RequestMapping("getList")
	@ResponseBody
	public HashMap<String, Object> getList(String startSiteName,String endSiteName, String departureDate) {
		List<ZtcClassesPriceForSale> lists = ztcClassesPriceForSaleManager.searchInfoLikes(startSiteName, endSiteName,departureDate);
		for(int i=0;i<lists.size();i++){
			ZtcClassesPriceForSale ztcClassesPriceForSale=lists.get(i);
			if(ztcClassesPriceForSale.getStatus()==1){
				continue;
			}
			setPreferentialStatus(ztcClassesPriceForSale);
		}
		HashMap<String, Object> wapper = new HashMap<>();
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", lists);
		return wapper;
	}

	/**
	 * 设置特惠状态
	 * @param ztcClassesPriceForSale 在售班次价格对象
	 */
    private void setPreferentialStatus(ZtcClassesPriceForSale ztcClassesPriceForSale) {
    	String price=ztcClassesPriceForSale.getPrice();
		String seatInfo=ztcClassesPriceForSale.getSeatInfo();
		String[] seatInfos=seatInfo.split("\\u0029,");
		int count=0;
		for(int i=0;i<seatInfos.length;i++){
			String seat=seatInfos[i];
			if ("".equals(seat)) {
				continue;
			}
			String[] tags=seat.split(",");
			if(Float.parseFloat(tags[2])<Float.parseFloat(price)){
				ztcClassesPriceForSale.setIsPreferential(1);
				break;
			}
			count++;
		}
		if(count==seatInfos.length){
			ztcClassesPriceForSale.setIsPreferential(0);
		}
	}

	/**
	 * 获取在售班次价格对象
	 * @param id 在售班次价格对象id
	 * @return 操作结果信息
	 */
	@RequestMapping("get")
	@ResponseBody
	public HashMap<String, Object> get(String id){
		ZtcClassesPriceForSale ztcClassesPriceForSale = new ZtcClassesPriceForSale();
		ztcClassesPriceForSale.setId(id);
		ZtcClassesPriceForSale ztcClassesPriceForSaleBean = ztcClassesPriceForSaleManager.searchInfoEquals(ztcClassesPriceForSale).get(0);
		setPreferentialStatus(ztcClassesPriceForSaleBean);
		ZtcClassesForSale ztcClassesForSale=new ZtcClassesForSale();
		ztcClassesForSale.setId(ztcClassesPriceForSaleBean.getFkClassesForSaleId());
		ZtcClassesForSale ztcClassesForSaleBean=ztcClassesForSaleManager.searchInfoEquals(ztcClassesForSale).get(0);
		ZtcClassesSiteForSale ztcClassesSite=new ZtcClassesSiteForSale();
		ztcClassesSite.setId(ztcClassesPriceForSaleBean.getFkStartClassesSiteForSaleId());
		ztcClassesSite.setSequence(0);
		ZtcClassesSiteForSale startSite=ztcClassesSiteForSaleManager.searchInfoEquals(ztcClassesSite).get(0);
		ztcClassesSite.setId(ztcClassesPriceForSaleBean.getFkEndClassesSiteForSaleId());
		ZtcClassesSiteForSale endSite=ztcClassesSiteForSaleManager.searchInfoEquals(ztcClassesSite).get(0);
		HashMap<String, Object> wapper = new HashMap<>();
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", ztcClassesPriceForSaleBean);
		wapper.put("classes", ztcClassesForSaleBean);
		wapper.put("startSite", startSite);
		wapper.put("endSite", endSite);
		return wapper;
	}

	/**
	 * 停售在售班次价格
	 * @param ids 在售班次价格id集合
	 * @param session
	 * @return 操作结果信息
	 */
	@RequestMapping("stopSell")
	@ResponseBody
	public HashMap<String,Object> stopSell(String ids,HttpSession session){
		HashMap<String, Object> wapper = new HashMap<>();
		Date now=new Date();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作班次");
			log.info("非运营公司或车方不可操作班次");
			return wapper;
		}
		if (transport.getStatus() == 4) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该用户公司已被冻结，不可停售班次");
			log.info("该用户公司已被冻结，不可停售班次");
			return wapper;
		}
		ids = ids.substring(1, ids.length() - 1);
		ids = ids.replace("\"", "");
		String[] idList=ids.split(",");
		for(int i=0;i<idList.length;i++){
			ZtcClassesPriceForSale priceForSale=ztcClassesPriceForSaleDao.get(idList[i]);
			if (priceForSale == null || priceForSale.getStatus() == 1) {
				continue;
			}
			String seatInfos = priceForSale.getSeatInfo();
			String[] seatInfo = seatInfos.split("\\u0029,");
			String newSeatInfo = "";
			for (int j = 0; j < seatInfo.length; j++) {
				String seat = seatInfo[j];
				if ("".equals(seat)) {
					continue;
				}
				int seatIndex = seat.indexOf(")");
				if (seatIndex > 0) {
					seat = seat.substring(1, seatIndex);
				} else {
					seat = seat.substring(1);
				}
				String[] tags = seat.split(",");
				if ("0".equals(tags[1])) {
					newSeatInfo += "(" + tags[0] + ",2," + tags[2] + "," + tags[3] + "),";
				} else {
					newSeatInfo += "(" + seat + "),";
				}
			}
			if (newSeatInfo.length() > 0) {
				newSeatInfo = newSeatInfo.substring(0, newSeatInfo.length() - 1);
			}
			if(!"".equals(newSeatInfo)&&!"()".equals(newSeatInfo)){
				priceForSale.setSeatInfo(newSeatInfo);
			}
			priceForSale.setStatus(1);
			priceForSale.setModifyTime(now);
			priceForSale.setFkModifyUserId(user.getId());
			String priceStatus = ztcClassesPriceForSaleDao.update(priceForSale);
			if ("0".equals(priceStatus)) {
				wapper.put("success", Boolean.valueOf(false));
				wapper.put("msg", "停售失败");
				log.info("停售失败");
				return wapper;
			}
		}
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", "停售成功");
		log.info("停售成功");
		return wapper;
	}

	/**
	 * 起售在售班次价格
	 * @param ids 在售班次价格id集合
	 * @param session
	 * @return 操作结果信息
	 */
	@RequestMapping("restartToSell")
	@ResponseBody
	public HashMap<String,Object> restartToSell(String ids,HttpSession session){
		HashMap<String, Object> wapper = new HashMap<>();
		Date now=new Date();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作班次");
			log.info("非运营公司或车方不可操作班次");
			return wapper;
		}
		if (transport.getStatus() == 4) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该用户公司已被冻结，不可停售班次");
			log.info("该用户公司已被冻结，不可停售班次");
			return wapper;
		}
		ids = ids.substring(1, ids.length() - 1);
		ids = ids.replace("\"", "");
		String[] idList=ids.split(",");
		for(int i=0;i<idList.length;i++){
			ZtcClassesPriceForSale priceForSale=ztcClassesPriceForSaleDao.get(idList[i]);
            if (priceForSale == null || priceForSale.getStatus() == 0) {
				continue;
			}
			ZtcClassesSiteForSale endSiteForSale=ztcClassesSiteForSaleDao.get(priceForSale.getFkEndClassesSiteForSaleId());
            if(endSiteForSale.getDepartureTime().getTime()<System.currentTimeMillis()){
                continue;
			}
			String seatInfos = priceForSale.getSeatInfo();
			String[] seatInfo = seatInfos.split("\\u0029,");
			String newSeatInfo = "";
			for (int j = 0; j < seatInfo.length; j++) {
				String seat = seatInfo[j];
				if ("".equals(seat)) {
					continue;
				}
				int seatIndex = seat.indexOf(")");
				if (seatIndex > 0) {
					seat = seat.substring(1, seatIndex);
				} else {
					seat = seat.substring(1);
				}
				String[] tags = seat.split(",");
				if ("2".equals(tags[1])) {
					newSeatInfo += "(" + tags[0] + ",0," + tags[2] + "," + tags[3] + "),";
				} else {
					newSeatInfo += "(" + seat + "),";
				}
			}
			if (newSeatInfo.length() > 0) {
				newSeatInfo = newSeatInfo.substring(0, newSeatInfo.length() - 1);
			}
			if(!"".equals(newSeatInfo)&&!"()".equals(newSeatInfo)){
				priceForSale.setSeatInfo(newSeatInfo);
			}
			priceForSale.setStatus(0);
			priceForSale.setModifyTime(now);
			priceForSale.setFkModifyUserId(user.getId());
			String priceStatus = ztcClassesPriceForSaleDao.update(priceForSale);
			if ("0".equals(priceStatus)) {
				wapper.put("success", Boolean.valueOf(false));
				wapper.put("msg", "起售失败");
				log.info("起售失败");
				return wapper;
			}
		}
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", "起售成功");
		log.info("起售成功");
		return wapper;
	}
}